package com.cloud.example.business.flow.controller.depend;

import com.cloud.async.flow.web.client.FlowTemplate;
import com.cloud.async.flow.worker.WorkResult;
import com.cloud.async.flow.wrapper.WorkerWrapper;
import com.cloud.example.business.flow.controller.depend.worker.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 后面请求依赖于前面请求的执行结果,Lambda表达式版本
 *
 * @author chengrui
 */
@RequestMapping("/api/flow")
@RestController
public class DependLambdaController {



    /**
     * logger
     */
    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    FlowTemplate flowTemplate;

    @GetMapping("/dependLambda")
    public String dependLambda() throws ExecutionException, InterruptedException {

        WorkerWrapper<WorkResult<User>, String> workerWrapper2 = new WorkerWrapper.Builder<WorkResult<User>, String>()
                .worker((WorkResult<User> result, Map<String, WorkerWrapper> allWrappers) -> {
                    logger.info("worker2 入参:" + result.getResult());
                    return result.getResult().getName();
                })
                .callback((boolean success, WorkResult<User> param, WorkResult<String> workResult) ->
                        logger.info("worker2 的结果是：" + workResult.getResult()))
                .id("third")
                .build();

        WorkerWrapper<WorkResult<User>, User> workerWrapper1 = new WorkerWrapper.Builder<WorkResult<User>, User>()
                .worker((WorkResult<User> result, Map<String, WorkerWrapper> allWrappers) -> {
                    logger.info("worker1 入参:" + result.getResult());
                    return new User("user1");
                })
                .callback((boolean success, WorkResult<User> param, WorkResult<User> workResult) ->
                        logger.info("worker1 的结果是：" + workResult.getResult()))
                .id("second")
                .next(workerWrapper2)
                .build();

        WorkerWrapper<String, User> workerWrapper = new WorkerWrapper.Builder<String, User>()
                .worker((String object, Map<String, WorkerWrapper> allWrappers) -> {
                    logger.info("worker0 入参:" + object);
                    return new User("user0");
                })
                .param("The default param ...")
                .id("first")
                .next(workerWrapper1, true)
                .callback((boolean success, String param, WorkResult<User> workResult) ->
                        logger.info("worker0 的结果是：" + workResult.getResult())
                )
                .build();

        //虽然尚未执行，但是也可以先取得结果的引用，作为下一个任务的入参。V1.2前写法，需要手工给
        //V1.3后，不用给wrapper setParam了，直接在worker的action里自行根据id获取即可.参考dependnew包下代码
        WorkResult<User> result = workerWrapper.getWorkResult();
        WorkResult<User> result1 = workerWrapper1.getWorkResult();
        workerWrapper1.setParam(result);
        workerWrapper2.setParam(result1);

        flowTemplate.startWork(3333, workerWrapper);

        logger.info("------end------");
        logger.info("worker0 的结果是：" + workerWrapper.getWorkResult());
        logger.info("worker1 的结果是：" + workerWrapper1.getWorkResult());
        logger.info("worker2 的结果是：" + workerWrapper2.getWorkResult());

        return "success!";
    }



}
